﻿@using SmartStore.Web.Models.Catalog;
@model IListActions
@{ 
	var pageable = Model.PagedList;
}

@functions
{
	private int GetFirstPageIndex(IPageable list)
	{
		int maxPagesToDisplay = 100;
		if ((list.TotalPages < maxPagesToDisplay) ||
			((list.PageIndex - (maxPagesToDisplay / 2)) < 0))
		{
			return 0;
		}
		if ((list.PageIndex + (maxPagesToDisplay / 2)) >= list.TotalPages)
		{
			return (list.TotalPages - maxPagesToDisplay);
		}
		return (list.PageIndex - (maxPagesToDisplay / 2));
	}

	private int GetLastPageIndex(IPageable list)
	{
		int maxPagesToDisplay = 100;
		int num = maxPagesToDisplay / 2;
		if ((maxPagesToDisplay % 2) == 0)
		{
			num--;
		}
		if ((list.TotalPages < maxPagesToDisplay) ||
			((list.PageIndex + num) >= list.TotalPages))
		{
			return (list.TotalPages - 1);
		}
		if ((list.PageIndex - (maxPagesToDisplay / 2)) < 0)
		{
			return (maxPagesToDisplay - 1);
		}
		return (list.PageIndex + num);
	}
}

@if (Model.AvailablePageSizes != null && Model.AvailablePageSizes.Count() > 1)
{
	<div class="artlist-action artlist-action--pagesize ml-auto">
		<div class="mf-dropdown">
			<button class="btn btn-secondary btn-artlist-action">
				<em>@T("Pager.XPerPage", "<span>" + pageable.PageSize + "</span>")</em>
				<i class="fas fa-caret-down"></i>
			</button>
			<select class="artlist-action-select form-control noskin"
					name="artlist-action-pagesize"
					id="artlist-action-pagesize"
					data-qname="s"
					data-url='@ModifyUrl(null, null, "i")'
					title="@T("Pager.XPerPage", pageable.PageSize)">
				@foreach (var size in Model.AvailablePageSizes)
				{
					<option value="@size" @Html.Attr("selected", "selected", size == pageable.PageSize)>@size</option>
				}
			</select>
		</div>
	</div>
}

@if (pageable.TotalPages > 1)
{
	<div class="artlist-action artlist-action--pager d-flex flex-nowrap">
		@if (pageable.PageNumber > 1)
		{
			<a href='@ModifyUrl(null, "i=" + (pageable.PageNumber - 1))' rel="prev" class="btn btn-secondary btn-pager-prev pull-xs-left" data-toggle="tooltip" title="@T("Pager.PageX", pageable.PageNumber - 1)">
				<i class="fa fa-chevron-left"></i>
			</a>
		}

		<div class="mf-dropdown">
			<button class="btn btn-secondary btn-artlist-action">
				<em class="d-sm-none"><span>@pageable.PageNumber</span>/<span>@pageable.TotalPages</span></em>
				<em class="d-none d-sm-inline d-md-none">@T("Pager.PageXOfYShort", "<span>" + pageable.PageNumber + "</span>", "<span>" + pageable.TotalPages + "</span>")</em>
				<em class="d-none d-md-inline">@T("Pager.PageXOfY", "<span>" + pageable.PageNumber + "</span>", "<span>" + pageable.TotalPages + "</span>")</em>
				<i class="fas fa-caret-down"></i>
			</button>
			<select class="artlist-action-select form-control noskin" name="artlist-action-page" id="artlist-action-page" data-qname="i" title="@T("Pager.PageXOfY", pageable.PageNumber, pageable.TotalPages)">
				@{
					int pageCount = pageable.TotalPages;
					int start = GetFirstPageIndex(pageable) + 1;
					int end = GetLastPageIndex(pageable) + 1;
				}
				@if (start > 3)
				{
					<option value="1" @Html.Attr("selected", "selected", 1 == pageable.PageNumber)>@T("Pager.PageXOfY", 1, pageCount)</option>
					<option disabled="disabled">...</option>
				}
				@for (var i = start; i <= end; i++)
				{
					<option value="@i" @Html.Attr("selected", "selected", i == pageable.PageNumber)>@T("Pager.PageXOfY", i, pageCount)</option>
				}
				@if (end < (pageCount - 3))
				{
					<option disabled="disabled">...</option>
					<option value="@pageCount" @Html.Attr("selected", "selected", pageCount == pageable.PageNumber)>@T("Pager.PageXOfY", pageCount, pageCount)</option>
				}
			</select>
		</div>

		@if (pageable.PageNumber < pageable.TotalPages)
		{
			<a href='@ModifyUrl(null, "i=" + (pageable.PageNumber + 1))' rel="next" class="btn btn-secondary btn-pager-next" data-toggle="tooltip" title="@T("Pager.PageX", pageable.PageNumber + 1)">
				<i class="fa fa-chevron-right"></i>
			</a>
		}
	</div>
}